package ru.susu.algebra.belonogov;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import ru.susu.algebra.pair.Pair;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.partition.PartitionsFactory;
import ru.susu.algebra.partition.filter.AlternatingPartitionsFilter;

/**
 * @author akargapolov
 * @since: 06.06.2009
 */
public class ProportionalCharactersSnAnViewer extends ProportionalCharactersViewerBase
{
	Map<Integer, List<Pair<Partition, Partition>>> anPartitions = new LinkedHashMap<Integer, List<Pair<Partition, Partition>>>();

	@Override
	protected void writePartitions() throws Exception
	{
		MapListsPartitionsWriter writer = new MapListsPartitionsWriter();
		writer.write(anPartitions, new AlternatingPartitionsFilter(), "ExecuteResults/SnAn/");
	}

	@Override
	protected void createPartitions() throws Exception
	{
		for (int i = 1; i <= MAX_VALUE; i++)
		{
			anPartitions.put(i, new ArrayList<Pair<Partition, Partition>>());
		}

		for (int k = 0; k < MAX_VALUE; k++)
		{
			addPairs(new Integer[0], k);

			Integer[] array = new Integer[1];
			array[0] = 1;
			if (addPairs(array, k) == null)
			{
				break;
			}
		}
	}

	protected Pair<Partition, Partition> addPairs(Integer[] array, int k)
	{
		Partition p1 = PartitionsFactory.createPartition(array);
		p1 = addBasePower(p1, 2, k);
		p1 = addOp.performOperation(p1, 3);

		Partition p2 = PartitionsFactory.createPartition(array);
		p2 = addBasePower(p2, 2, k);
		Integer[] tmp = p2.getArrayRepresentation();
		int first, second;
		if (array.length == 0)
		{
			first = 2;
			second = 1;
		}
		else
		{
			first = 1;
			second = 2;
		}
		p2 = increaseOp.performOperation(p2, tmp.length - k - 1, first);
		p2 = increaseOp.performOperation(p2, tmp.length - k - 2, second);

		if (p2.getPresentedNumber() > MAX_VALUE)
		{
			return null;
		}

		Pair<Partition, Partition> pair = new Pair<Partition, Partition>(p1, p2);
		anPartitions.get(p1.getPresentedNumber()).add(pair);
		return pair;
	}



}
